package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.ClassCourseDto;
import com.yqg.education.entity.ClassCourse;
import com.yqg.education.service.IClassCourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassCourseController 前端控制器
 * @author zxf
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/class-course")
public class ClassCourseController {

    @Resource
    private IClassCourseService classCourseService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody ClassCourse classCourse) {
        return Result.success(classCourseService.saveOrUpdate(classCourse));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
        return Result.success(classCourseService.removeById(id));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ClassCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<ClassCourse> list=  classCourseService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(classCourseService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<ClassCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(classCourseService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @PostMapping("/addList")
    public Result addList(@RequestBody ClassCourseDto classCourseDto) {
        ArrayList<ClassCourse> classCourses = new ArrayList<>();
        List<Long> courseIds = classCourseDto.getCourseIds();
        for (Long courseId:courseIds) {
            LambdaQueryWrapper<ClassCourse> classCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            classCourseLambdaQueryWrapper.eq(ClassCourse::getClassId, classCourseDto.getClassId());
            classCourseLambdaQueryWrapper.eq(ClassCourse::getCourseId, courseId);
            ClassCourse one = classCourseService.getOne(classCourseLambdaQueryWrapper);
            if (one == null) {
                ClassCourse classCourse = new ClassCourse();
                classCourse.setClassId(classCourseDto.getClassId());
                classCourse.setCourseId(courseId);
                classCourses.add(classCourse);
            }
        }
        return Result.success(classCourseService.saveOrUpdateBatch(classCourses));
    }
    /**
     * 替换某班级绑定的指定课程为其他未绑定的
     */
    @PostMapping("/replaceCourseByClassId")
    public Result replaceCourseByClassId(@RequestBody ClassCourse classCourse) {
        return Result.success(classCourseService.saveOrUpdate(classCourse));
    }
}

